#ifndef __MULTIPOLYGON_H__
#define __MULTIPOLYGON_H__

#pragma once

#include "Polygon.h"
#include "Wf.h"

//#include ".\selectedpolygon.h"

typedef loop_list<CPolygon *> polygon_list;

class CSelectedPolygon;
class CGroupedPolygon;

class CMultiPolygon
{
private:
	polygon_list	m_PolygonSet;

	CSelectedPolygon	*m_pSelectedPolygons;	// only used for drawing by order

	CGroupedPolygon		*m_pGroupedPolygons;
public:
	CMultiPolygon(void);
	~CMultiPolygon(void);

public:
	CPolygon* GetPolygon(int Index) const;
	CSelectedPolygon* GetSelectdPolygons(void)	const {return m_pSelectedPolygons;}
	int		NumPolygon() const;
	bool	InsertPolygonAt(int Index,CPolygon* poly);
	bool	AddPolygon(CPolygon* poly, bool bAppend );
	bool	RemovePolygon(int Index);	

	void	Clear();
	void	Zoom( double ratio );
	void	SetSelectedPolygons( CSelectedPolygon	*pSel ) { m_pSelectedPolygons=pSel; }
	void	Connect( int nPoly1, int nPoly2 );

	void	SetGroupedPolygons( CGroupedPolygon	*pSel ) { m_pGroupedPolygons=pSel; }
	
	CPolygon* operator[](int Index)
	{
		int size = (int) m_PolygonSet.size();
		while(Index >= size)
		{
			Index -= size; 
		}
		while(Index < 0)
		{
			Index += size;
		}
		assert(Index >= 0 && Index < size);

		return m_PolygonSet[Index];		
	}
	CString m_fname;
	int		SaveToWFFile( char *filename );
	int		SaveToPLYFile( char *filename );
	int		LoadWFFile( char *filename, bool bAdd );
	int		GetFromWF( CWFObject &wf );
	int		LoadPLYFile( char *filename, bool bAdd );
	int		LoadScriptFile( char *filename, bool bAdd );
	int		LoadScriptFilePLY( char *filename, bool bAdd );

	void	ChangeOrder( int idx, int offset );
	void	ResetFilledColor(bool bAll);
	void	Split( int &nPoly, int &nVert );
	void	AlignOnTopObject(BOOL bAlignTangent);

	void	Draw( CDC *pDC, BOOL bDrawVertex, BOOL bColored, bool bDrawNormal);
	void	DrawHandles( CDC *pDC );
	void	ShowVertexIndices( CDC *pDC );

	int		FindFirstPolyContainPoint( double px, double py );
	int		FindFirstVertexOnPoint( int &nPolyIndex, int &nVertexIndex, double px, double py );
	int		FindFirstVertexOnEdge( int &nPolyIndex, int &nVertexIndex, double &ratio, double px, double py );

	void DrawInGL(CSize window_size, float fGLViewWidth, float fGLViewHeight,bool bDrawCurve, bool bDrawVertex,bool bDrawNormal,CPseudoColorRGB &PsdC,Vector2 Origin=Vector2(0,0));
	void	GetMuiltPolyPointsXY(std::vector<std::vector<float> > &dstXs, std::vector<std::vector<float> > &dstYs){
		int nP= NumPolygon();		
		dstXs.clear();	dstXs.resize(nP);
		dstYs.clear();	dstYs.resize(nP);
		for (int i=0; i<nP; ++i){
			CPolygon *pP = GetPolygon(i);			pP->GetAllPointsXY(dstXs[i],dstYs[i]);
		}
	}
	void	GetMultiPolyNormalV2(std::vector<std::vector<Vector2> > &dst){
		int nP= NumPolygon();		
		dst.clear();	dst.resize(nP);
		for (int i=0; i<nP; ++i){
			CPolygon *pP = GetPolygon(i);			pP->GetAllNormalV2(dst[i]);
		}
	}
	void ResampleByAvgEdgeLen(double Avglen){
		int nP= NumPolygon();		
		for (int i=0; i<nP; ++i){
			CPolygon *pP = GetPolygon(i);
			pP->ResampleByAvgEdgeLen(Avglen);
		}
	}
};

#endif